package com.scau.dao.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.scau.dao.BaseDao;
import com.scau.dao.IPaymentDao;
import com.scau.vo.Admin;
import com.scau.vo.Payment;
import com.scau.vo.SaleOrder;

/**
 * @author Jinglun
 * @create 2017年4月22日
 */
public class PaymentDaoImpl extends BaseDao implements IPaymentDao {

	private Session session = null;
	private Transaction tx = null;
	private String hql = "";
	
	@Override
	public boolean savePayment(Payment payment) {
		session = getSession();
		tx = session.beginTransaction();
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		String id1 = request.getParameter("payment.stuff");
		Integer stuff = Integer.parseInt(id1);

		String saleON = request.getParameter("payment.saleOrderNo");
		System.out.println("11111:"+saleON);
		
		String sql = "insert into payment"+
        "(stuff, amount, payDateTime, payType, paymentNo, saleOrderNo) "+
	    "values"+
	        "("+stuff+",'"+payment.getAmount()+"','"+payment.getPayDateTime()+"','" +
	    payment.getPayType()+"','"+payment.getPaymentNo()+"','"+saleON+"')";
		
		System.out.println("插入的sql语句："+sql);
		
		Query query2 = session.createSQLQuery(sql);
		
		int a = query2.executeUpdate();
		if(a>0){
			System.out.println("success");
		}
		else{
			System.out.println("fail");
		}

		tx.commit();
		session.close();
		System.out.println("支付表结束，表示成功");
		return true;

	}

	@Override
	public List<Payment> listPayment() {
		session = getSession();
		
		hql = "from Payment payment";
		Query query = session.createQuery(hql);
		List<Payment> payList = query.list();
		session.close();
		return payList;
	}

}
